from flask import render_template, jsonify, session, redirect, url_for, current_app, request
from .blueprint import common


@common.route('/')
@common.route('/demo/<page>')
def index(page=None):
    tpl = 'index.html'
    if page is not None and page != 'index.html':
        tpl = 'demo/' + page
    return render_template(tpl)


@common.route('/G6')
@common.route('/Z6')
def redirect_to_machining_shop():
    line_name = request.path.replace('/', '')
    return redirect('/NEP/PSS/MachiningShop?line_name=' + line_name)


@common.route('/ALS/EnvironmentData.html')
def redirect_to_foundry_final_inspection():
    return redirect('/NEP/ALS/FoundryFinalInspection')


@common.route('/nep')
def mobile():
    tpl = 'm/index.html'
    return render_template(tpl)


@common.route('/m/machining')
def mobile_machining():
    tpl = 'm/machining.html'
    return render_template(tpl)


@common.route('/m/foundry')
def mobile_foundry():
    tpl = 'm/foundry.html'
    return render_template(tpl)


@common.route('/m/assembly')
def mobile_assembly():
    tpl = 'm/assembly.html'
    return render_template(tpl)


@common.route('/m/grid_table')
def grid_table():
    tpl = 'm/grid_table.html'
    return render_template(tpl)


@common.route('/m/icon_tab_bar')
def icon_tab_bar():
    tpl = 'm/icon_tab_bar.html'
    return render_template(tpl)


@common.route('/m/NEP/MachiningShop', methods=['GET'])
def machining_shop():
    import time
    line_name = request.args.get('line_name')
    if line_name == 'G6' or line_name == 'Z6':
        items = {
            'line_name': line_name,
            'time': int(time.time())
        }
        tpl_name = 'm/zp_pss_' + line_name.lower() + '.html'
        # tpl_name = 'm/zp_pss_g6.html'
        return render_template(tpl_name, items=items)


@common.route('/NEP_Production_Viewer')
def nep_production_viewer():
    tpl = 'NEP_Production_Viewer.html'
    return render_template(tpl)


@common.route('/cat')
def bba_production_overview():
    tpl = 'BBAProductionOverview.html'
    return render_template(tpl)


@common.route('/NEPCockpit')
def nep_cockpit():
    tpl = 'm/NEPCockpit.html'
    return render_template(tpl)


@common.route('/test')
def test():
    import time
    stime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
    fn = 'hello.txt'
    with open('app/static/data/' + fn, 'w') as f:
        f.write(stime)
    return stime


@common.route('/easy_auth', methods=['GET'])
def easy_auth():
    import time
    token = request.args.get('token')
    date_str = time.strftime('%Y%m%d')
    auth_ok = 'failed'
    if token == date_str:
        auth_ok = 'success'
    return auth_ok
